﻿<%@ Page Language="C#" MasterPageFile="../../main.master" Inherits="VioletDevelopmentLaboratory.Net.Web.PageBase" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Collections.ObjectModel" %>

<%@ Register src="modify.ascx" tagname="modify" tagprefix="uc1" %>

<script runat="server">
	private Board currentBoard;
	protected Int64 TotalFileLength;

	protected void Page_Load(object sender, EventArgs e)
	{
		if (Request.UrlReferrer == null)
		{
			Response.Redirect("list.aspx");
		}

		currentBoard = Board.GetBoard(Request.QueryString["id"]);
		
		if (!Page.IsPostBack)
		{
			TempFileGridViewBind();
		}
	}
	
	protected void TempFileGridViewBind()
	{
		TotalFileLength = 0L;
		TempFileGridView.DataSource = AttachmentFileManager.GetTempAttachmentFileInfo(currentBoard.ComponentID, ComponentType.Board);
		TempFileGridView.DataBind();
		TotalFileLengthLiteral.Text = VUtility.GetFormatFileLength(TotalFileLength);
	}

	protected void TempFileGridView_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			AttachmentFileInfo attachmentFileInfo = (AttachmentFileInfo)e.Row.DataItem;

			((CheckBox)e.Row.FindControl("FixedDeleteCheckBox")).Checked = attachmentFileInfo.UploadFile.PhysicalFile.CreationTime.AddHours(1) < DateTime.Now;

			TotalFileLength += attachmentFileInfo.UploadFile.ContentLength;
		}
	}

	protected void RemoveButton_Click(object sender, EventArgs e)
	{
		foreach (GridViewRow row in TempFileGridView.Rows)
		{
			if (((CheckBox)row.FindControl("FixedDeleteCheckBox")).Checked)
			{
				AttachmentFileManager.Remove(currentBoard.ComponentID, TempFileGridView.DataKeys[row.RowIndex].Value.ToString(), ComponentType.Board);
			}
		}

		VUtility.ShowMessagePanel(Page, "제거되었습니다.", true);
		
		TempFileGridViewBind();
	}
	protected void SelectedRemoveButton_Click(object sender, EventArgs e)
	{
		foreach (GridViewRow row in TempFileGridView.Rows)
		{
			if (((CheckBox)row.FindControl("SelectCheckBox")).Checked)
			{
				AttachmentFileManager.Remove(currentBoard.ComponentID, TempFileGridView.DataKeys[row.RowIndex].Value.ToString(), ComponentType.Board);
			}
		}

		VUtility.ShowMessagePanel(Page, "제거되었습니다.", true);

		TempFileGridViewBind();
	}

	protected void ReloadButton_Click(object sender, EventArgs e)
	{
		TempFileGridViewBind();
	}
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="BodyContentPlaceHolder" runat="server">

	<h2>게시판 &gt; 목록 &gt; <%= String.Format("{0} ({1})", currentBoard.ComponentID, currentBoard.Name) %> &gt; 임시첨부파일</h2>

	<uc1:modify ID="modify1" runat="server" />

	<fieldset>
		<legend>임시첨부파일</legend>
		<div>
			<asp:Button ID="RemoveButton" runat="server" OnClick="RemoveButton_Click"
				OnClientClick="<%$ Resources:VGlobalResources, General_Script_RemoveConfirm %>" Text="제거확정제거" />
			<asp:Button ID="SelectedRemoveButton" runat="server" OnClick="SelectedRemoveButton_Click"
				OnClientClick="<%$ Resources:VGlobalResources, General_Script_RemoveConfirm %>" Text="선택강제제거" />
			<br />
			<span style="color : #ff0000;">
				※ 주의 : 모든 첨부파일은 서버로 업로드 되면 제거대기 상태로 등록이 됩니다.
				이후에 사용자가 글등록을 완료하면 제거대기가 해제 됩니다.<br />
				'제거확정'은 파일이 서버에 업로드된 후 1시간동안 제거대기가 해제되지 않았거나 실제파일이 존재하지 않는 첨부파일입니다.
			</span>
		</div>

		<div>총 파일 크기 : <asp:Literal ID="TotalFileLengthLiteral" runat="server" /></div>

		<asp:GridView ID="TempFileGridView" runat="server" AutoGenerateColumns="False" 
			DataKeyNames="FileID" OnRowDataBound="TempFileGridView_RowDataBound"
			ShowFooter="True" Width="100%" CellPadding="5" PageSize="20" 
			EmptyDataText="No Rows">
			<FooterStyle BackColor="Gray" />
			<Columns>
				<asp:TemplateField>
					<HeaderTemplate>
						<input id="SelectAllCheckBox" type="checkbox" value="" onclick="javascript:VWebGlobal_SelectCheckBox(this, '<%= TempFileGridView.ClientID %>', 'SelectCheckBox');" /><label for="SelectAllCheckBox">전체</label>
					</HeaderTemplate>
					<ItemTemplate>
						<asp:CheckBox ID="SelectCheckBox" runat="server" />
					</ItemTemplate>
					<HeaderStyle Width="50px" />
					<ItemStyle HorizontalAlign="Center" />
				</asp:TemplateField>
				<asp:TemplateField HeaderText="파일이름">
					<ItemTemplate>
						<dl style="margin : 0; padding : 0;">
							<dt>원본 이름</dt>
							<dd><%# Eval("UploadFile.originalFileName")%></dd>
							<dt>저장된 이름</dt>
							<dd><%# Eval("UploadFile.SavedFileName")%></dd>
						</dl>
					</ItemTemplate>
				</asp:TemplateField>
				<asp:TemplateField HeaderText="타입">
					<ItemTemplate><%# Eval("UploadFile.ContentType")%></ItemTemplate>
				</asp:TemplateField>
				<asp:TemplateField HeaderText="크기">
					<ItemTemplate>
						<%# VUtility.GetFormatFileLength(Eval("UploadFile.ContentLength").ToString())%>
					</ItemTemplate>
					<HeaderStyle Width="120px" />
					<ItemStyle HorizontalAlign="Right" />
				</asp:TemplateField>
				<asp:TemplateField HeaderText="날짜">
					<ItemTemplate>
						<dl style="margin : 0; padding : 0;">
							<dt>저장된 날짜</dt>
							<dd><%# Eval("UploadFile.PhysicalFile.CreationTime")%></dd>
							<dt>제거확정 날짜</dt>
							<dd><%# ((DateTime)Eval("UploadFile.PhysicalFile.CreationTime")).AddHours(1D)%></dd>
						</dl>
					</ItemTemplate>
				</asp:TemplateField>
				<asp:TemplateField HeaderText="실제파일존재">
					<ItemTemplate>
						<%# Eval("UploadFile.PhysicalFile.Exists")%>
					</ItemTemplate>
				</asp:TemplateField>
				<asp:TemplateField HeaderText="제거확정">
					<ItemTemplate>
						<asp:CheckBox ID="FixedDeleteCheckBox" runat="server" Enabled="false" />
					</ItemTemplate>
					<HeaderStyle Width="60px" />
					<ItemStyle HorizontalAlign="Center" />
				</asp:TemplateField>
			</Columns>
			<HeaderStyle BackColor="Gray" />
			<AlternatingRowStyle BackColor="LightGray" />
		</asp:GridView>
	</fieldset>

</asp:Content>